package bgate.hung.framework;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
	protected Bitmap bitmap;
	protected Rect takeRect;
	protected int numbOfFrame;
	protected int currentFrame;
	protected long lastTimeFrameDisplayed;
	protected int timePerFrame;
	protected int width, height;
	protected int x, y;

	protected Sprite(Bitmap bitmap, int x, int y, int timePerFrame,
			int numbsFrame) {
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		currentFrame = 0;
		numbOfFrame = numbsFrame;
		width = bitmap.getWidth() / numbsFrame;
		height = bitmap.getHeight();
		takeRect = new Rect(0, 0, width, height);
		this.timePerFrame = timePerFrame;
		lastTimeFrameDisplayed = 0l;
		takeRect = new Rect(0, 0, width, height);
	}
	
	protected Sprite(Bitmap bitmap, int timePerFrame,
			int numbsFrame) {
		this.bitmap = bitmap;	
		currentFrame = 0;
		numbOfFrame = numbsFrame;
		width = bitmap.getWidth() / numbsFrame;
		height = bitmap.getHeight();
		takeRect = new Rect(0, 0, width, height);
		this.timePerFrame = timePerFrame;
		lastTimeFrameDisplayed = 0l;
	}
	
	protected Sprite(int x, int y, int timePerFrame, int numbsFrame) {
		this.x = x;
		this.y = y;
		numbOfFrame = numbsFrame;
		this.timePerFrame = timePerFrame;
	}
	
	protected void initialize() {
		currentFrame = 0;
		width = bitmap.getWidth() / numbOfFrame;
		height = bitmap.getHeight();
		takeRect = new Rect(0, 0, width, height);
		lastTimeFrameDisplayed = 0l;
		takeRect = new Rect(0, 0, width, height);
	}
	
	protected Sprite() {
		
	}
	
	protected Bitmap getImageFrame(int frame) {
		return Bitmap.createBitmap(bitmap, frame * width, 0, width, height);
	}

	public int getX() {
		return x;
	}

	public void setX(int x){
		this.x=x;
	}
	public int getY() {
		return y;
	}

	public void setY(int y){
		this.y=y;
	}
	public int getWidth() {
		return width;
	}

	public void draw(Canvas canvas) {
		Rect destRect = new Rect(getX(), getY(), getX() + width, getY()
				+ height);
		canvas.drawBitmap(bitmap, takeRect, destRect, null);
	}

	public void update(long gameTime) {
		if (gameTime > lastTimeFrameDisplayed + timePerFrame) {
			lastTimeFrameDisplayed = gameTime;
			currentFrame++;
			 if (currentFrame >= numbOfFrame)
			 currentFrame = 0;
		}
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;

	}

	public int getCurrentFrame() {
		return currentFrame;
	}

	public void nextFrame() {
		if (currentFrame < numbOfFrame - 1)
			currentFrame++;
		else
			currentFrame = 0;
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}

	public void previousFrame() {
		if (currentFrame > 0)
			currentFrame--;
		else
			currentFrame = numbOfFrame - 1;
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}
	
	public void nextFrame(long gameTime) {
		if (gameTime > lastTimeFrameDisplayed + timePerFrame) {
			lastTimeFrameDisplayed = gameTime;
			currentFrame++;	
			if (currentFrame >= numbOfFrame)
				 currentFrame = 0;			
		}
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}
	
	public void nextFrame(long gameTime,int reStartFrame) {
		if (gameTime > lastTimeFrameDisplayed + timePerFrame) {
			lastTimeFrameDisplayed = gameTime;
			currentFrame++;	
			if (currentFrame >= numbOfFrame)
				 currentFrame = reStartFrame;			
		}
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}

	public void previousFrame(long gameTime) {
		if (gameTime > lastTimeFrameDisplayed + timePerFrame) {
			lastTimeFrameDisplayed = gameTime;
			currentFrame--;	
			if (currentFrame <= 0)
				 currentFrame = numbOfFrame-1;			
		}
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}
	
	public void setFrame(int frame){
		currentFrame=frame;
		this.takeRect.left = currentFrame * width;
		this.takeRect.right = this.takeRect.left + width;
	}

	

	
}

